<?php
namespace app\api\controller;
use app\model\WxUassistant as WxUassistantModel;
use app\model\Token as TokenModel;
use think\Db;


class WxUassistant extends Base
{   
    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function indexByMember(){
        $user_id = $this->login_user['user_id'];
        $member_id = Db::name('wx_member')->where('user_id',$user_id)->value('id');
        if (empty($member_id)) {
            return apiResult('2000','获取成功',[]);
        }

        $map_params = [
            
        ];
        $map = getSearchParam($map_params);
        $map[] = ['member_id','=',$member_id];
        
        $model = new WxUassistantModel();
        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = $model->where($map)->order('id','desc')->paginate($page_param)->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['msg_uid'] = empty($vo['member_id']) ? $vo['wx_assistant_id'] : 'member_'.$vo['member_id'];
            $list['data'][$k]['user'] = Db::name('user')->field('nickname,mobile,headimgurl')->where('id',$vo['user_id'])->find();
        }
        return apiResult('2000','获取成功',$list);
    }

    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function index(){
        $user_id = $this->login_user['user_id'];
        $map_params = [
            ['key'=>'member_category_id','type'=>'='],
            ['key'=>'type','type'=>'='],
        ];
        $map = getSearchParam($map_params);
        if ( isSearchParam('keyword_search') ) {
            $map[] = ['name','like','%'.input('keyword_search').'%'];
        }
        $order_type = 'sort desc,id desc';

        $model = new WxUassistantModel();
        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = $model->where($map)->order($order_type)->paginate($page_param)->toarray();
        foreach ($list['data'] as $k => $vo) {
            
        }
        return apiResult('2000','获取成功',$list);
    }

    /**
     * 获取详情
     * @Author   cch
     * @DateTime 2020-05-26T17:22:45+0800
     * @return   [type]                   [description]
     */
    public function read($id){
        $model = new WxUassistantModel();
        $data = $model->where('id',$id)->find();
        if (!empty($data)) {
            
        }
        
        return apiResult('2000','获取成功',$data);
    }

    /**
     * 获取详情
     * @Author   cch
     * @DateTime 2020-05-26T17:22:45+0800
     * @return   [type]                   [description]
     */
    public function myMember(){
        $user_id = $this->login_user['user_id'];
        $model = new WxUassistantModel();
        $data = $model->where('user_id',$user_id)->find();
        if (!empty($data)) {

        }
        
        return apiResult('2000','获取成功',$data);
    }

 

    /**
     * 点赞
     * @Author   cch
     * @DateTime 2020-05-26T17:22:45+0800
     * @return   [type]                   [description]
     */
    public function like($id){
        $model = new WxUassistantModel();
        $model->where('id',$id)->inc('like_num')->update();
        
        return apiResult('2000','点赞成功');
    }

    
    public function save(){
        $user_id = $this->login_user['user_id'];
        $model = new WxUassistantModel();
        $fields = [
            'must'=>['name','member_category_id','image','description'],
            'nomust'=>['position','company','resource','requirement','greet','tags']
        ];
        $save_data = paramFilter(request()->param(),$fields);
        if (!empty($save_data['error_msg'])) {
            return apiResult('5000',$save_data['error_msg']);
        }

        $id = $model->where('user_id',$user_id)->value('id');
        if (!empty($id)) {
            $model->where('id',$id)->update($save_data);
        }else{
            $save_data['ctime'] = time();
            $save_data['type'] = 1;
            $save_data['user_id'] = $user_id;

            $save_data['wx_assistant_id'] = Db::name('wx_assistant')->min('id');
            // $save_data['api_url'] = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro';
            // $save_data['api_param'] = '{"client_id":"YeyMF21dXex0vmVjUP5Z044G","client_secret":"IoFoQbHWLmf0c8t9VGXM9baYqIOqfbD7"}';

            $id = $model->insertGetId($save_data);
        }
        
        $data = $model->where('id',$id)->find();
        return apiResult('2000','保存成功',$data);
    }

   


}
